package Homework;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/*5.设计4个线程，其中两个线程每次对j增加1，另外两个线程对j每次减少1。考虑到线程的
安全性写出程序。（不要使用atomaicInteger 来做）*/
public class work08 {
    public static void main(String[] args) {
        MyThread2 thread = new MyThread2();

        // 创建一个固定大小为4的线程池
        ExecutorService executorService = Executors.newFixedThreadPool(4);

        // 向线程池提交递增任务
        for (int i = 0; i < 2; i++) {
            executorService.submit(() -> {
                for (int k = 0; k < 10; k++) {
                    thread.increaceJ();
                }
            });
        }

        // 向线程池提交递减任务
        for (int i = 0; i < 2; i++) {
            executorService.submit(() -> {
                for (int k = 0; k < 10; k++) {
                    thread.decreaceJ();
                }
            });
        }

        // 关闭线程池
        executorService.shutdown();
    }
}
